package liumengbaio.lolzhangyoubao.Activity.utils;

import android.content.Context;

import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.InFo.NextFreeHeroInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.WeekFree;

/**
 * 获取Assent中的数据
 * Created by lanouhn on 16/8/25.
 */
public class NextAssetsUtils {


    /**
     * 获取下期周免英雄的方法
     *
     * @param list 本周免费英雄的id
     * @return
     */
    public static List<NextFreeHeroInfo> getNextFreeHeroList(Context context, List<WeekFree.DataBean.NextperiodBean> list) {
        List<NextFreeHeroInfo> nfhList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            NextFreeHeroInfo nfhinfo = getHeroInfo(context, list.get(i).getId());

            nfhList.add(nfhinfo);
        }
        return nfhList;

    }

    private static NextFreeHeroInfo getHeroInfo(Context context, String id) {
        NextFreeHeroInfo nfhinfo = new NextFreeHeroInfo();
        try {
            InputStream is = context.getAssets().open("hero/" + id + ".json");
            // Read the entire asset into a local byte buffer.
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            is.close();

            // Convert the buffer into a string.
            String text = new String(buffer, "UTF-8");

            JSONObject hJson = new JSONObject(text);

            nfhinfo.setNickname(hJson.getString("nickname"));
            nfhinfo.setPic_url(hJson.getString("pic_url"));
            nfhinfo.setMoney(hJson.getString("money"));
            nfhinfo.setPoint(hJson.getString("point"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return nfhinfo;

    }




}
